package com.bocai.grainshop.order.web;

import com.bocai.common.exception.HasWareException;
import com.bocai.common.utils.PageUtils;
import com.bocai.common.utils.R;
import com.bocai.grainshop.order.service.OrderService;
import com.bocai.grainshop.order.vo.OrderConfirmVo;
import com.bocai.grainshop.order.vo.OrderSubmitVo;
import com.bocai.grainshop.order.vo.SubmitOrderResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Controller
public class OrderWebController {

    @Autowired
    private OrderService orderService;

    /**
     * @Author: SunJinbo
     * @Description  获取结算页面信息
     * @Param [model]
     * @return java.lang.String
     * @Data Created in 2022/4/22 21:02
     */
    @GetMapping("/toTrade")
    public String toTrade(Model model){
        OrderConfirmVo confirmVo = orderService.OrderConfirm();
        model.addAttribute("confirmOrderData",confirmVo);
        return "confirm";
    }

    /**
     * @Author: SunJinbo
     * @Description  下订单  提交订单
     * @Param [orderSubmitVo]
     * @return java.lang.String
     * @Data Created in 2022/4/22 20:37
     */
    @PostMapping("/submitOrder")
    public String sunmbitOrder(OrderSubmitVo orderSubmitVo, Model model, RedirectAttributes redirectAttributes){
        log.info("订单提交的数据---->{}"+orderSubmitVo.toString());
        //创建订单  验令牌 验价格 锁库存
        try{
            SubmitOrderResponseVo responseVo = orderService.createOrder(orderSubmitVo);
            if(responseVo.getCode() == 200){
                model.addAttribute("submitOrderResp",responseVo);
                return "pay";
            }else {
                String msg = "";
                switch (responseVo.getCode()){
                    case 0: msg = "库存不足,无法下单";
                        break;
                    case 1: msg = "验价失败,请重新下单";
                        break;
                    case 2: msg = "请勿重复提交订单";
                        break;
                }
                redirectAttributes.addFlashAttribute("msg",msg);
                return "redirect:http://order.grainshop.com/toTrade";
            }
        }catch (Exception e){
            if(e instanceof HasWareException){
                String message = e.getMessage();
                redirectAttributes.addFlashAttribute("msg",message);
            }
            return "redirect:http://order.grainshop.com/toTrade";
        }
    }

    /**
     * 支付完成获取的订单列表
     */
    @RequestMapping("/listWithItem")
    public String listWithItem(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,Model model){
        Map<String,Object> map = new HashMap<>();
        map.put("page",pageNum.toString());
        PageUtils page = orderService.queryWithItem(map);
        model.addAttribute("page",page);
        return "list";
    }

    /**
     * @Author: SunJinbo
     * @Description  普通情况查询订单列表
     * @Param
     * @return
     * @Data Created in 2022/5/4 22:42
     */
    @RequestMapping("/listWith")
    public String listWith(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,Model model){
        Map<String,Object> map = new HashMap<>();
        map.put("page",pageNum.toString());
        PageUtils page = orderService.queryWith(map);
        model.addAttribute("page",page);
        return "list";
    }
}
